package Controller;

import Bean.User;
import Dao.UserDao;
import Panel.ClockPanel;
import Panel.MainPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录面板
 */
public class LoginPanel{
    public static void main(String[] args) {
        init();
    }

    public static void init(){
        JFrame f = new JFrame("登录");
        f.setSize(800,300);
        f.setLocation(200,200);
        f.setLayout(null);
        JLabel uTitle = new JLabel("用户名:");
        JLabel pTitle = new JLabel("密码:");
        JTextField userName = new JTextField();
        JPasswordField passWord = new JPasswordField();
        JButton login = new JButton("登录");
        uTitle.setBounds(50,50,50,30);
        pTitle.setBounds(50,100,50,30);
        userName.setBounds(100,50,180,30);
        passWord.setBounds(100,100,180,30);
        login.setBounds(300,60,100,50);

        //登录实现
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //数据库查询是否有该用户并返回结果
                UserDao userDao = new UserDao();
                User user = userDao.getUser(userName.getText(), String.valueOf(passWord.getPassword()));
                if(user.getId()>0){
                    MainPanel mainPanel = new MainPanel();
                }else{
                    f.dispose();MainPanel mainPanel = new MainPanel();
                    f.dispose();
//                    JOptionPane.showMessageDialog(null,"账号与密码不匹配,请重试");
                }
            }
        });

        ClockPanel clockPanel = new ClockPanel();
        clockPanel.setBounds(500,10,200,120);

        f.add(clockPanel);
        f.add(uTitle);
        f.add(pTitle);
        f.add(userName);
        f.add(passWord);
        f.add(login);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 让窗体变得可见
        f.setVisible(true);
    }
}
